<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 数码商城管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
        }

        .auth-container {
            background-color: rgba(255, 255, 255, 0.95);
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
            backdrop-filter: blur(10px);
        }

        .auth-title {
            text-align: center;
            margin-bottom: 1.5rem;
            color: #333;
            font-weight: 600;
            font-size: 1.5rem;
        }

        .form-group {
            margin-bottom: 1rem;
            position: relative;
        }

        .form-control {
            width: 100%;
            padding: 0.75rem 1rem;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 1rem;
            transition: border-color 0.2s, box-shadow 0.2s;
        }

        .form-control:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .form-label {
            position: absolute;
            left: 1rem;
            top: 0.75rem;
            color: #666;
            transition: all 0.2s;
            pointer-events: none;
            background: white;
            padding: 0 0.25rem;
        }

        .form-control:focus+.form-label,
        .form-control:not(:placeholder-shown)+.form-label {
            top: -0.5rem;
            left: 0.75rem;
            font-size: 0.875rem;
            color: #667eea;
        }

        .password-toggle {
            position: absolute;
            right: 1rem;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            color: #666;
            user-select: none;
        }

        .btn {
            width: 100%;
            padding: 0.75rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .btn:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }

        .btn:active {
            transform: translateY(0);
        }

        .checkbox-group {
            display: flex;
            align-items: center;
            margin-bottom: 1rem;
            gap: 0.5rem;
        }

        .checkbox-group input[type="checkbox"] {
            width: 1rem;
            height: 1rem;
        }

        .error-message {
            color: #dc3545;
            font-size: 0.875rem;
            margin-top: 0.5rem;
            text-align: center;
            display: none;
        }

        .success-message {
            color: #28a745;
            font-size: 0.875rem;
            margin-top: 0.5rem;
            text-align: center;
            display: none;
        }

        .auth-switch {
            text-align: center;
            margin-top: 1rem;
            color: #666;
            font-size: 0.875rem;
        }

        .auth-switch a {
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
            cursor: pointer;
        }

        .auth-switch a:hover {
            text-decoration: underline;
        }

        .form-container {
            position: relative;
        }

        .register-form {
            display: none;
        }

        .form-container.active .login-form {
            display: none;
        }

        .form-container.active .register-form {
            display: block;
        }

        .loading {
            opacity: 0.6;
            pointer-events: none;
        }

        .spinner {
            display: inline-block;
            width: 1rem;
            height: 1rem;
            border: 2px solid #ffffff;
            border-radius: 50%;
            border-top-color: transparent;
            animation: spin 1s ease-in-out infinite;
        }

        @keyframes spin {
            to {
                transform: rotate(360deg);
            }
        }

        @media (max-width: 480px) {
            .auth-container {
                margin: 1rem;
                padding: 1.5rem;
            }
        }
    </style>
</head>

<body>
    <div class="auth-container">
        <h2 class="auth-title">数码商城管理系统</h2>
        <div class="form-container" id="formContainer">
            <!-- 登录表单 -->
            <form id="loginForm" class="login-form">
                <div class="form-group">
                    <input type="text" class="form-control" id="loginUsername" name="username" placeholder=" " required>
                    <label class="form-label" for="loginUsername">用户名</label>
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" id="loginPassword" name="password" placeholder=" "
                        required>
                    <label class="form-label" for="loginPassword">密码</label>
                    <span class="password-toggle" onclick="togglePassword('loginPassword')">👁</span>
                </div>
                <div class="checkbox-group">
                    <input type="checkbox" id="rememberMe">
                    <label for="rememberMe">记住我</label>
                </div>
                <button type="submit" class="btn" id="loginBtn">
                    <span id="loginBtnText">登录</span>
                    <span id="loginBtnSpinner" class="spinner" style="display: none;"></span>
                </button>
                <div id="loginErrorMessage" class="error-message"></div>
            </form>

            <!-- 注册表单 -->
            <form id="registerForm" class="register-form">
                <div class="form-group">
                    <input type="text" class="form-control" id="registerUsername" name="username" placeholder=" "
                        required>
                    <label class="form-label" for="registerUsername">用户名</label>
                </div>
                <div class="form-group">
                    <input type="email" class="form-control" id="registerEmail" name="email" placeholder=" " required>
                    <label class="form-label" for="registerEmail">邮箱</label>
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" id="registerPassword" name="password" placeholder=" "
                        required>
                    <label class="form-label" for="registerPassword">密码</label>
                    <span class="password-toggle" onclick="togglePassword('registerPassword')">👁</span>
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" id="confirmPassword" name="confirmPassword"
                        placeholder=" " required>
                    <label class="form-label" for="confirmPassword">确认密码</label>
                    <span class="password-toggle" onclick="togglePassword('confirmPassword')">👁</span>
                </div>
                <button type="submit" class="btn" id="registerBtn">
                    <span id="registerBtnText">注册</span>
                    <span id="registerBtnSpinner" class="spinner" style="display: none;"></span>
                </button>
                <div id="registerErrorMessage" class="error-message"></div>
                <div id="registerSuccessMessage" class="success-message"></div>
            </form>
        </div>
        <div class="auth-switch">
            <span id="switchText">还没有账号？</span>
            <a id="switchBtn">立即注册</a>
        </div>
    </div>

    <script>
        // 页面加载完成后执行
        document.addEventListener('DOMContentLoaded', function () {
            initializeApp();
        });

        function initializeApp() {
            // 切换登录/注册表单
            document.getElementById('switchBtn').addEventListener('click', function () {
                const container = document.getElementById('formContainer');
                const isLogin = !container.classList.contains('active');
                //切换css类实现表单显示或隐藏
                container.classList.toggle('active');
                //更新提示文本
                document.getElementById('switchText').textContent = isLogin ? '已有账号？' : '还没有账号？';
                this.textContent = isLogin ? '立即登录' : '立即注册';

                // 清空错误信息
                clearMessages();
                clearForms();
            });

            // 登录表单提交
            document.getElementById('loginForm').addEventListener('submit', handleLogin);

            // 注册表单提交
            document.getElementById('registerForm').addEventListener('submit', handleRegister);

            // 检查是否有保存的用户名(记住我)
            const savedUsername = localStorage.getItem('username');
            if (savedUsername) {
                document.getElementById('loginUsername').value = savedUsername;
                document.getElementById('rememberMe').checked = true;
            }
        }

        // 登录处理函数
        async function handleLogin(e) {
            // 阻止默认提交行为
            e.preventDefault();

            const username = document.getElementById('loginUsername').value.trim();
            const password = document.getElementById('loginPassword').value.trim();
            const rememberMe = document.getElementById('rememberMe').checked;

            if (!username || !password) {
                showError('loginErrorMessage', '请填写用户名和密码');
                return;
            }

            setLoading('loginBtn', true);

            try {
                const formData = new URLSearchParams();
                formData.append('username', username);
                formData.append('password', password);

                const response = await fetch('/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: formData
                });

                const data = await response.json();

                if (data.success) {
                    // 登录成功
                    if (rememberMe) {
                        localStorage.setItem('username', username);
                    } else {
                        localStorage.removeItem('username');
                    }

                    // 根据用户角色重定向
                    if (data.user && data.user.role === 1) {
                        window.location.href = '/houtai.html';
                    } else {
                        window.location.href = '/product.html';
                    }
                } else {
                    showError('loginErrorMessage', data.message || '登录失败');
                }
            } catch (error) {
                console.error('Login error:', error);
                showError('loginErrorMessage', '网络错误，请稍后重试');
            } finally {
                setLoading('loginBtn', false);
            }
        }
        // 注册处理函数
        async function handleRegister(e) {
            e.preventDefault();
            const username = document.getElementById('registerUsername').value.trim();
            const email = document.getElementById('registerEmail').value.trim();
            const password = document.getElementById('registerPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            if (!username || !email || !password || !confirmPassword) {
                showError('registerErrorMessage', '请填写所有字段');
                return;
            }

            if (password !== confirmPassword) {
                showError('registerErrorMessage', '两次输入的密码不一致');
                return;
            }

            if (password.length < 6) {
                showError('registerErrorMessage', '密码长度不能少于6位');
                return;
            }

            setLoading('registerBtn', true);

            try {
                // 使用FormData格式发送请求，匹配后端@RequestParam参数
                const formData = new URLSearchParams();
                formData.append('username', username);
                formData.append('email', email);
                formData.append('password', password);
                formData.append('confirmPassword', confirmPassword);

                const response = await fetch('/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: formData
                });

                const data = await response.json();

                if (data.success) {
                    showSuccess('registerSuccessMessage', '注册成功！请登录');
                    // 清空表单
                    document.getElementById('registerForm').reset();
                    // 1.5秒后切换到登录表单
                    setTimeout(() => {
                        document.getElementById('switchBtn').click();
                    }, 1500);
                } else {
                    showError('registerErrorMessage', data.message || '注册失败');
                }
            } catch (error) {
                console.error('Register error:', error);
                showError('registerErrorMessage', '网络错误，请稍后重试');
            } finally {
                setLoading('registerBtn', false);
            }
        }

        function togglePassword(inputId) {
            const input = document.getElementById(inputId);
            // 切换输入框类型
            input.type = input.type === 'password' ? 'text' : 'password';
        }

        function setLoading(btnId, loading) {
            const btn = document.getElementById(btnId);
            const text = document.getElementById(btnId + 'Text');
            const spinner = document.getElementById(btnId + 'Spinner');

            if (loading) {
                btn.classList.add('loading');
                text.style.display = 'none';
                spinner.style.display = 'inline-block';
            } else {
                btn.classList.remove('loading');
                text.style.display = 'inline';
                spinner.style.display = 'none';
            }
        }

        function showError(elementId, message) {
            const element = document.getElementById(elementId);
            element.textContent = message;
            element.style.display = 'block';
            setTimeout(() => {
                element.style.display = 'none';
            }, 5000);
        }

        function showSuccess(elementId, message) {
            const element = document.getElementById(elementId);
            element.textContent = message;
            element.style.display = 'block';
            setTimeout(() => {
                element.style.display = 'none';
            }, 5000);
        }

        function clearMessages() {
            document.querySelectorAll('.error-message, .success-message').forEach(el => {
                el.style.display = 'none';
            });
        }

        function clearForms() {
            document.getElementById('loginForm').reset();
            document.getElementById('registerForm').reset();
        }
    </script>
</body>

</html>